package com.sunxiansheng.home.controller;

import com.sunxiansheng.home.feign.SkuControllerFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Description: 首页控制器
 * @Author sun
 * @Create 2024/7/27 15:40
 * @Version 1.0
 */
@Slf4j
@RestController
@RefreshScope // 开启Nacos动态配置
public class HomeController {

    /**
     * 注入 Feign 客户端
     */
    @Resource
    private SkuControllerFeignService skuControllerFeignService;

    /**
     * 注入 RestTemplate
     */
    @Resource
    private RestTemplate restTemplate;

    /**
     * 测试读取Nacos配置
     */
    @Value(value = "${myconfig.test}")
    private String testConfig;

    /**
     * 请求地址
     */
    private static final String URL = "http://localhost:8082/test";

    @GetMapping("/getSkuInfo")
    public String getSkuInfo() {
        // 调用 sku 服务
        return restTemplate.getForObject(URL, String.class);
    }

    /**
     * 测试读取Nacos配置
     * @return
     */
    @GetMapping("/testConfig")
    public String testConfig() {
        return testConfig;
    }

    /**
     * 测试 Feign 客户端
     * @return
     */
    @GetMapping("/testFeign")
    public String testFeign() {
        return skuControllerFeignService.home();
    }

}